<html>
<head>
  <meta charset="utf-8">
  <!-- NOTE:  Don't change this Title -->
  <title>Blockly Frame</title>

<!--   <link type="text/css" rel="stylesheet" href="./blockly.css"> -->

  <!-- From closure_library.goog/ -->
<!--   <script type="text/javascript" src="./base.js"></script>   -->

  <script type="text/javascript" src="./jquery-1.9.1.min.js"></script>
  <!-- blockly-all.js is copied from appinventor/build/blocklyeditor -->
  <!-- blockly-toolbox.js is a modified version of blockly's core toolbox.js -->
  <script type="text/javascript" src="./blockly-all.js"></script>
  <script type="text/javascript" src="./blockly-toolbox.js"></script>

  <!-- Blockly javascript evaluators, modified for App Inventor -->
  <script type="text/javascript" src="./javascript.js"></script>
  <script type="text/javascript" src="./javascript/logic.js"></script>
  <script type="text/javascript" src="./javascript/control.js"></script>
  <script type="text/javascript" src="./javascript/procedures.js"></script>
  <script type="text/javascript" src="./javascript/math.js"></script>
  <script type="text/javascript" src="./javascript/lists.js"></script>
  <script type="text/javascript" src="./javascript/text.js"></script>
  <script type="text/javascript" src="./javascript/variables.js"></script>
  <script type="text/javascript" src="./javascript/componentblock.js"></script>

  <!-- quizme-initblocklyeditor.js is revised version of Blockly.BlocklyEditor.startup  --> 
  <script type="text/javascript" src="quizme-initblocklyeditor.js"></script>
  <script type="text/javascript" src="quizme.js"></script>
  <script type="text/javascript" src="quizme-helper.js"></script>
  <script type="text/javascript" src="./quizme-components.js"></script>
  <script type="text/javascript" src="./component-types.js"></script>
  <script type="text/javascript" src="./makequiz-helper.js"></script>
  <script type="text/javascript" src="./quizme-eval.js"></script>
<!--   <script type="text/javascript" src="./activity-quizme.js"></script> -->

  <!--  This should be loaded last b/c it overrides certain built-in functions. -->
  <script type="text/javascript" src="./overrides.js"></script>

  <style>
    html, body {
      background-color: #fff;
      margin: 0;
      padding: 0;
      overflow: hidden;
    }
    .blocklySvg {
      height: 100%;
      width: 100%;
    }
  </style>

    <script>
      // NOTE: Blockly injects a static toolbox in the form of an Xml tree. We want our toolbox to be dynamic but 
      //  we must initialize it here. The TOOLBOX category serves as a static heading for the toolbox.
      function initBlockly() {
        
        // This should probably be cleaned up, but there are several places where the quizname is available
        //   depending on where blockly.html is used.
        var quizname; 

        // Try to find quizname in the parameters (quizly's standalone set up)
        var href = window.parent.document.location.href;
        var arglist = href.split('?')[1];
        if (arglist) {
          var params = arglist.split('&');
          for (var i = 0; i < params.length; i++) {
             var keyval = params[i].split('=');
             if (keyval[0] == 'quizname')
               quizname = keyval[1];
          }
        }

        // Try to find quizname on the window -- this could be unified for CB
        if (!quizname) {
           if (window.parent.quiz) {
             quizname = window.parent.quiz.name;    // As a CB lesson component
           } else if (window.parent.activity) {
             quizname = window.parent.activity.quizType  // As a CB activity
           }
        }

        // Typeblocking configuration
	var typeblock_config = {
	  frame: 'ai_frame',
	  typeBlockDiv: 'ai_type_block',
	  inputText: 'ac_input_text'
	};

        Blockly.inject(document.body,
            {path: './',
             backpack:false,
             trashcan: true,
             toolbox: '<xml id="toolbox" style="display:none"><category name="TOOLBOX"><block type="bogus"></block></category></xml>'
            });

	 //This is what Blockly's init function does when passing options.
	 //We are overriding the init process so putting it here
	 goog.mixin(Blockly, {
	   collapse : true,
	   configForTypeBlock: typeblock_config
	 });

	//This would also be done in Blockly init, but we need to do it here cause of
	//the different init process in drawer (it'd be undefined at the time it hits
	//init in Blockly)
	if (Blockly.editable)
	  Blockly.TypeBlock(Blockly.configForTypeBlock);


        // Let the top-level application know that Blockly is ready.
        // Do any Quizme initializations here.
        window.parent.blocklyLoaded(Blockly);


        if (window.parent.document.title == "Quiz Maker Utility")
          initQuizMaker('./');
        else 
          initQuizme(quizname, './', arglist);
      }
    </script>

</head>
<body id="ai_frame" onload="initBlockly()">
 <div id="ai_type_block" style="display:none">
  <p>
    <input id="ac_input_text" />
  </p>
 </div>
</body>
</html>
